package com.csii.autocode.window;

import com.csii.autocode.entity.TableVo;
import com.csii.autocode.service.impl.CodeGenerateOne;
import com.csii.autocode.utils.DbReadTableUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class CodeWindow extends JFrame {
    private static final long b = -5324160085184088010L;

    private static String entity_package = "test";

    private static String entity_name = "TestEntity";

    private static String table_name = "t00_company";

    private static String ftl_description = "分公司";

    private static Integer field_row_num = 1;

    private static String primary_key_policy = "uuid";

    private static String sequence_code = "";

    String[] primkeys = new String[]{"uuid", "identity", "sequence"};

    /**
     * 创建windows界面
     */
    public CodeWindow() {
        JPanel jPanel = new JPanel();
        setContentPane(jPanel);
        jPanel.setLayout(new GridLayout(14, 2));
        JLabel jLabel1 = new JLabel("提示:");
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel("包名（小写）：");
        JTextField jTextField1 = new JTextField();
        JLabel jLabel4 = new JLabel("实体类名（首字母大写）：");
        JTextField jTextField2 = new JTextField();
        JLabel jLabel5 = new JLabel("表名：");
        JTextField jTextField3 = new JTextField(20);
        JLabel jLabel6 = new JLabel("主键生成策略：");
        JComboBox<String> jComboBox = new JComboBox<>(this.primkeys);
        jComboBox.setEnabled(false);
        JLabel jLabel7 = new JLabel("主键SEQUENCE：(oracle序列名)");
        JTextField jTextField4 = new JTextField(20);
        JLabel jLabel8 = new JLabel("功能描述：");
        JTextField jTextField5 = new JTextField();
        JLabel jLabel9 = new JLabel("行字段数目：");
        JTextField jTextField6 = new JTextField();
        jTextField6.setText(field_row_num + "");
        ButtonGroup buttonGroup = new ButtonGroup();
        JRadioButton jRadioButton1 = new JRadioButton("抽屉风格表单");
        jRadioButton1.setSelected(true);
        JRadioButton jRadioButton2 = new JRadioButton("弹窗风格表单");
        buttonGroup.add(jRadioButton1);
        buttonGroup.add(jRadioButton2);
        JCheckBox jCheckBox1 = new JCheckBox("Control");
        jCheckBox1.setSelected(true);
        JCheckBox jCheckBox2 = new JCheckBox("Vue");
        jCheckBox2.setSelected(true);
        JCheckBox jCheckBox3 = new JCheckBox("Service");
        jCheckBox3.setSelected(true);
        JCheckBox jCheckBox4 = new JCheckBox("Mapper.xml");
        jCheckBox4.setSelected(true);
        JCheckBox jCheckBox5 = new JCheckBox("Dao");
        jCheckBox5.setSelected(true);
        JCheckBox jCheckBox6 = new JCheckBox("Entity");
        jCheckBox6.setSelected(true);
        jPanel.add(jLabel1);
        jPanel.add(jLabel2);
        jPanel.add(jLabel3);
        jPanel.add(jTextField1);
        jPanel.add(jLabel4);
        jPanel.add(jTextField2);
        jPanel.add(jLabel5);
        jPanel.add(jTextField3);
        jPanel.add(jLabel6);
        jPanel.add(jComboBox);
        jPanel.add(jLabel7);
        jPanel.add(jTextField4);
        jPanel.add(jLabel8);
        jPanel.add(jTextField5);
        jPanel.add(jLabel9);
        jPanel.add(jTextField6);
        jPanel.add(jCheckBox1);
        jPanel.add(jCheckBox2);
        jPanel.add(jCheckBox3);
        jPanel.add(jCheckBox4);
        jPanel.add(jCheckBox5);
        jPanel.add(jCheckBox6);
        jPanel.add(jRadioButton1);
        jPanel.add(jRadioButton2);
        JButton jButton1 = new JButton("生成");
        jButton1.addActionListener((ActionEvent e) -> {
            if (!"".equals(jTextField1.getText())) {
                CodeWindow.entity_package = (jTextField1.getText());
            } else {
                jLabel2.setForeground(Color.red);
                jLabel2.setText("包名不能为空！");
                return;
            }
            if (!"".equals(jTextField2.getText())) {
                CodeWindow.entity_name = (jTextField2.getText());
            } else {
                jLabel2.setForeground(Color.red);
                jLabel2.setText("实体类名不能为空！");
                return;
            }
            if (!"".equals(jTextField5.getText())) {
                CodeWindow.ftl_description = (jTextField5.getText());
            } else {
                jLabel2.setForeground(Color.red);
                jLabel2.setText("描述不能为空！");
                return;
            }
            if (!"".equals(jTextField3.getText())) {
                CodeWindow.table_name = (jTextField3.getText());
            } else {
                jLabel2.setForeground(Color.red);
                jLabel2.setText("表名不能为空！");
                return;
            }
            CodeWindow.primary_key_policy = (String) jComboBox.getSelectedItem();
            if (CodeWindow.primary_key_policy.equals("sequence"))
                if (!"".equals(jTextField4.getText())) {
                    CodeWindow.sequence_code = (jTextField4.getText());
                } else {
                    jLabel2.setForeground(Color.red);
                    jLabel2.setText("主键生成策略为sequence时，序列号不能为空！");
                    return;
                }
            try {
                boolean bool = DbReadTableUtil.isExist(CodeWindow.table_name);
                if (bool) {
                    TableVo tableVo = new TableVo();
                    tableVo.setTableName(CodeWindow.table_name);
                    tableVo.setPrimaryKeyPolicy(CodeWindow.primary_key_policy);
                    tableVo.setEntityPackage(CodeWindow.entity_package);
                    tableVo.setEntityName(CodeWindow.entity_name);
                    tableVo.setFieldRowNum(CodeWindow.field_row_num);
                    tableVo.setSequenceCode(CodeWindow.sequence_code);
                    tableVo.setFtlDescription(CodeWindow.ftl_description);
                    (new CodeGenerateOne(tableVo)).generateCodeFile(null);
                    jLabel2.setForeground(Color.red);
                    jLabel2.setText("成功生成增删改查->功能：" + CodeWindow.ftl_description);
                } else {
                    jLabel2.setForeground(Color.red);
                    jLabel2.setText("表[" + CodeWindow.table_name + "] 在数据库中，不存在");
                    System.err.println(" ERROR ：   表 [ " + CodeWindow.table_name + " ] 在数据库中，不存在 ！请确认数据源配置是否配置正确、表名是否填写正确~ ");
                }
            } catch (Exception exception) {
                jLabel2.setForeground(Color.red);
                jLabel2.setText(exception.getMessage());
            }
        });
        JButton jButton2 = new JButton("退出");
        jButton2.addActionListener((ActionEvent e) -> {
            CodeWindow.this.dispose();
            System.exit(0);
        });
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        setTitle("代码生成器[单表模型]");
        setVisible(true);
        setDefaultCloseOperation(3);
        setSize(new Dimension(600, 400));
        setResizable(false);
        setLocationRelativeTo(getOwner());
    }

    /**
     * 详细使用手册： http://doc.csii.com/1275846
     */
    public static void main(String[] args) {
        try {
            (new CodeWindow()).pack();
        } catch (Exception exception) {
            System.out.println(exception.getMessage());
        }
    }
}
